package com.itheima.stock.service.impl;


import com.itheima.stock.cache.Cache;
import com.itheima.stock.constant.Constants;
import com.itheima.stock.dto.LoginDTO;
import com.itheima.stock.entity.SysUser;
import com.itheima.stock.enums.CodeEnum;
import com.itheima.stock.exception.Exceptions;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.service.UserService;
import com.itheima.stock.util.IdWorkers;
import com.itheima.stock.util.RedisKeyUtils;
import com.itheima.stock.vo.CaptchaVo;
import com.itheima.stock.vo.LoginVO;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

@Service
public class UserServiceImpl implements UserService {

    private final IdWorkers idWorkers;

    private final Cache cache;

    private final SysUserMapper sysUserMapper;

    private static final int FORE = 4;
    private final PasswordEncoder passwordEncoder;

    public UserServiceImpl(IdWorkers idWorkers, Cache cache, SysUserMapper sysUserMapper, PasswordEncoder passwordEncoder) {
        this.idWorkers = idWorkers;
        this.cache = cache;
        this.sysUserMapper = sysUserMapper;
        this.passwordEncoder = passwordEncoder;
    }

    @Override
    public CaptchaVo captcha() {

        //生成四位数的验证码
        String captcha = RandomStringUtils.randomNumeric(FORE);
        //将验证码存储到redis中
        //1、先生成一个key
        String redisKey = RedisKeyUtils.generateKey(Constants.CAPTCHA_PREFIX, idWorkers.nextStrId());
        //2、将验证码存储到redis中(key设计   架构设计)
        cache.set(redisKey, captcha, Constants.CAPTCHA_EXPIRE_TIME);
        //3、数据组装返回
        return CaptchaVo.builder()
                .rkey(redisKey)
                .code(captcha)
                .build();
    }

   @Override
    public LoginVO login(LoginDTO dto) {
        if(ObjectUtils.isEmpty(dto)
            ||ObjectUtils.isEmpty(dto.getUsername())
            ||ObjectUtils.isEmpty(dto.getPassword())
            ||ObjectUtils.isEmpty(dto.getCode())
            ||ObjectUtils.isEmpty(dto.getRKey())){
            Exceptions.cast(CodeEnum.DATA_IS_NOT_NULL);
        }
        //判断验证码是否正确
        String captchaInRedis = cache.get(dto.getRKey());
        if(!StringUtils.equals(captchaInRedis,dto.getCode())){
            Exceptions.cast(CodeEnum.SYSTEM_VERIFY_CODE_ERROR);
        }
        //删除redis中的验证码
        cache.remove(dto.getRKey());
        //如果验证码正确，去数据库中查询用户信息
        SysUser user = sysUserMapper.selectByUsername(dto.getUsername(),Constants.SYS_USER_STATUS_NORMAL,Constants.SYS_USER_UN_DELETE);
        //判断用户是否为空，或者判断密码是否正确
        if(ObjectUtils.isEmpty(user)||!passwordEncoder.matches(dto.getPassword(),user.getPassword())){
            Exceptions.cast(CodeEnum.SYSTEM_PASSWORD_ERROR);
        }

        return LoginVO.builder()
                .id(user.getId())
                .username(user.getUsername())
                .nickName(user.getNickName())
                .phone(user.getPhone())
                .build();
    }
}
